package cn.iocoder.yudao.module.trade.controller.admin.orderpostconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.module.trade.controller.admin.orderpostconfig.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.orderpostconfig.OrderPostConfigDO;
import cn.iocoder.yudao.module.trade.service.orderpostconfig.OrderPostConfigService;

@Tag(name = "管理后台 - 订单回传扣量配置")
@RestController
@RequestMapping("/trade/order-post-config")
@Validated
public class OrderPostConfigController {

    @Resource
    private OrderPostConfigService orderPostConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建订单回传扣量配置")
    @PreAuthorize("@ss.hasPermission('trade:order-post-config:create')")
    public CommonResult<Integer> createOrderPostConfig(@Valid @RequestBody OrderPostConfigSaveReqVO createReqVO) {
        return success(orderPostConfigService.createOrderPostConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单回传扣量配置")
    @PreAuthorize("@ss.hasPermission('trade:order-post-config:update')")
    public CommonResult<Boolean> updateOrderPostConfig(@Valid @RequestBody OrderPostConfigSaveReqVO updateReqVO) {
        orderPostConfigService.updateOrderPostConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单回传扣量配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-post-config:delete')")
    public CommonResult<Boolean> deleteOrderPostConfig(@RequestParam("id") Long id) {
        orderPostConfigService.deleteOrderPostConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单回传扣量配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-post-config:query')")
    public CommonResult<OrderPostConfigRespVO> getOrderPostConfig(@RequestParam("id") Long id) {
        OrderPostConfigDO orderPostConfig = orderPostConfigService.getOrderPostConfig(id);
        return success(BeanUtils.toBean(orderPostConfig, OrderPostConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单回传扣量配置分页")
    @PreAuthorize("@ss.hasPermission('trade:order-post-config:query')")
    public CommonResult<PageResult<OrderPostConfigRespVO>> getOrderPostConfigPage(@Valid OrderPostConfigPageReqVO pageReqVO) {
        PageResult<OrderPostConfigDO> pageResult = orderPostConfigService.getOrderPostConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderPostConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单回传扣量配置 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-post-config:export')")
    public void exportOrderPostConfigExcel(@Valid OrderPostConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderPostConfigDO> list = orderPostConfigService.getOrderPostConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单回传扣量配置.xls", "数据", OrderPostConfigRespVO.class,
                        BeanUtils.toBean(list, OrderPostConfigRespVO.class));
    }

}